﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using ModeloGaleriaNET.Properties;
using MySql.Data.MySqlClient;

namespace ModeloGaleriaNET
{
    public class DBConnData
    {
        /// <summary>
        /// Ordenador donde se encuentra el SGBD.
        /// </summary>
        private String server;
        /// <summary>
        /// Nombre de usuario
        /// </summary>
        private String user;
        /// <summary>
        /// Contraseña
        /// </summary>
        private String password;
        /// <summary>
        /// Base de datos a utilizar dentro del SGBD
        /// </summary>
        private String schema;

        /// <summary>
        /// El constructor es privado para garantizar que los usuarios no puedan crear instancias de la clase.
        /// </summary>
        private DBConnData()
        {
            this.server = Settings.Default.DBServer;
            this.user = Settings.Default.DBUser;
            this.schema = Settings.Default.DBSchema;
            this.password = Utilities.DesencriptarRSA(Settings.Default.DBPass);
        }

        /// <summary>
        /// Instancia singleton de la clase DBConnectionData
        /// </summary>
        public static readonly DBConnData Instance = new DBConnData();

        public String Server
        {
            get { return server; }
            set { server = value; }
        }

        public String User
        {
            get { return user; }
            set { user = value; }
        }

        public String Password
        {
            get { return password; }
            set { password = value; }
        }

        public String Schema
        {
            get { return schema; }
            set { schema = value; }
        }

        public String ConnectionString
        {
            get
            {
                MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
                builder.Server = server;
                builder.UserID = user;
                builder.Password = password;
                builder.Database = schema;

                return builder.ConnectionString;
            }
        }

        public String EntityConnectionString
        {
            get
            {
                String metadata = Resources.EntityMetadata;
                String provider = Resources.EntityProvider;

                return "metadata=" + metadata + ";" +
                       "provider=" + provider + ";" +
                       "provider connection string=\"" + ConnectionString + "\";";
            }
        }

        public void Save()
        {
            Settings.Default.DBServer = server;
            Settings.Default.DBUser = user;
            Settings.Default.DBPass = Utilities.EncriptarRSA(password);
            Settings.Default.DBSchema = schema;
            Settings.Default.Save();
        }

        public void Clear()
        {
            server = string.Empty;
            user = string.Empty;
            password = string.Empty;
            schema = string.Empty;
            Save();
        }

        public bool IsEmpty()
        {
            return string.IsNullOrWhiteSpace(user) ||
                   string.IsNullOrWhiteSpace(server) ||
                   string.IsNullOrWhiteSpace(password) ||
                   string.IsNullOrWhiteSpace(schema);
        }

        public bool TestConnection()
        {
            bool salida = true;
            MySqlConnection conn = new MySqlConnection(ConnectionString);

            try
            {
                conn.Open();

            }
            catch (MySqlException)
            {
                salida = false;
            }
            catch (Exception)
            {
                salida = false;
            }
            finally
            {
                conn.Close();
            }

            return salida;
        }
    }
}
